package com.example.homeworkmutualevaluation.Controller;

import com.example.homeworkmutualevaluation.Entity.Course;
import com.example.homeworkmutualevaluation.Entity.Refanswer;
import com.example.homeworkmutualevaluation.Service.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Resource
    private StudentService studentService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private CourseService courseService;
    @Resource
    private TeachingService teachingService;
    @Resource
    private SCService scService;
    @Resource
    private SubmitService submitService;
    @Resource
    private HomeworkService homeworkService;
    @Resource
    private RefanswerService refanswerService;
    @Resource
    private EvaluationService evaluationService;
    @Resource
    private DiscussService discussService;

    @PostMapping("/count_student")
    public Map<String, Object> countStudent(@RequestParam String cno){
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", scService.countByCno(cno));
        return response;
    }

    @PostMapping("/get_students")
    public Map<String, Object> getStudents(@RequestParam String cno){
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", studentService.findAllByCno(cno));
        return response;
    }

    @PostMapping("/count_teacher")
    public Map<String, Object> countTeacher(@RequestParam String cno){
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", teachingService.countByCno(cno));
        return response;
    }

    @PostMapping("/get_teachers")
    public Map<String, Object> getTeachers(@RequestParam String cno){
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", teacherService.findAllByCno(cno));
        return response;
    }

    @PostMapping("/all_course")
    public Map<String, Object> allCourse(){
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", courseService.findAll());
        return response;
    }

    @PostMapping("/get_homeworks")
    public Map<String, Object> getHomeworks(@RequestParam String cno){
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", homeworkService.findAllByCno(cno));
        return response;
    }

    @PostMapping("/get_homework")
    public Map<String, Object> getHomework(@RequestParam int homeworkid){
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", homeworkService.findById(homeworkid));
        return response;
    }

    @PostMapping("/get_submitted")
    public Map<String, Object> getSubmitted(@RequestParam String sphone, @RequestParam int homeworkid) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", submitService.findBySphoneAndHomeworkid(sphone, homeworkid));
        return response;
    }

    @PostMapping("/get_refanswer")
    public Map<String, Object> getRefanswer(@RequestParam int homeworkid) {
        HashMap<String, Object> response = new HashMap<>();
        Refanswer refanswer = refanswerService.findByHomeworkid(homeworkid);
        if (refanswer != null) {
            response.put("code", 1);
            response.put("data", refanswer.getContent());
        } else {
            response.put("code", 0);
            response.put("msg", "参考答案不存在");
        }
        return response;
    }

    @PostMapping("/get_evaluations_by_others")  // 别人批改你的
    public Map<String, Object> getEvaluationsByOthers(@RequestParam int homeworkid, @RequestParam String submitter) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", evaluationService.findAllBySubmitter(homeworkid, submitter));
        return response;
    }

    @PostMapping("/get_evaluations_by_self")    // 你批改别人的
    public Map<String, Object> getEvaluationsBySelf(@RequestParam int homeworkid, @RequestParam String evaluator) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", evaluationService.findAllByEvaluator(homeworkid, evaluator));
        return response;
    }

    @PostMapping("/get_all_evaluations_by_self")    // 你批改别人的
    public Map<String, Object> getAllEvaluationsBySelf(@RequestParam String evaluator) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", evaluationService.findAllByEvaluator(evaluator));
        return response;
    }

    @PostMapping("/discuss_homework")
    public Map<String, Object> discussHomework(@RequestParam String sphone, @RequestParam int homeworkid, @RequestParam String content) {
        HashMap<String, Object> response = new HashMap<>();
        String discussid;
        if (submitService.findBySphoneAndHomeworkid(sphone, homeworkid) == null) {
            response.put("code", 0);
            response.put("msg", "提交作业后才允许讨论");
        } else if ((discussid = discussService.insert(sphone, homeworkid, content)) != null) {
            response.put("code", 1);
            response.put("discussid", discussid);
        } else {
            response.put("code", 0);
            response.put("msg", "未知错误");
        }
        return response;
    }

    @PostMapping("/get_all_discuss")
    public Map<String, Object> getAllDiscuss(@RequestParam int homeworkid) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", discussService.findAllByHomeworkid(homeworkid));
        return response;
    }

    @PostMapping("/del_discuss")
    public Map<String, Object> getAllDiscuss(@RequestParam String discussid) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", discussService.delete(discussid)?1:0);
        return response;
    }

    @PostMapping("/get_course_by_homeworkid")
    public Map<String, Object> getCourseByHomeworkid(@RequestParam int homeworkid) {
        HashMap<String, Object> response = new HashMap<>();
        Course course = courseService.findByHomeworkid(homeworkid);
        response.put("code", (course!=null)?1:0);
        response.put("course", course);
        return response;
    }
}
